/********************************************************************
Name: Table 1
Author: Dan Thompson
Date: September 2021
Description: 

********************************************************************/

clear all
set matsize 11000
set maxvar 15000
gl path = "~/Dropbox/Mariel Effects"

* Loop over the different populations included in the analysis 
// note: 252 counties, 505 counties, 1513 counties, all counties, and Florida counties
qui foreach g in 252 505 1513 9999 florida {

	* Loop over the inclusion or exclusion of county-specific time trends
	forval t=0/1 {
		
		* Bring in the county analysis file
		use "$path/Replication/data/county_analysis_data.dta", clear
		drop if year>1980
		if "`g'"!="florida" keep if rank<=`g'
		if "`g'"=="florida" keep if state=="florida"
		
		* Run the regression for the with Miami as the treated unit
		if `t'==0 reghdfe rep_vs miami_1980, absorb(rank year) vce(cluster rank)
		if `t'==1 reghdfe rep_vs miami_1980, absorb(i.rank##c.year year) vce(cluster rank)
		
		* Store the regression output
		local b`g'_`t' = _b[miami_1980] // estimated effect
		local e`g'_`t' = e(N)/e(N_clust) // number of elections included
		local n`g'_`t' = e(N_clust) // number of elections included
		
		* Set up a matrix to store the placebo effects
		distinct rank
		matrix Taus = J(r(ndistinct), 1, .)
		
		* Loop over the other units and compute their placebo treatment effect using
		* the same regression
		sort rank year
		levelsof rank
		local c = 1
		foreach i in `r(levels)' {
			if `i'>1 drop if rank==1
			cap drop tmp_treat
			gen tmp_treat = rank==`i' & year>=1980
			if `t'==0 reghdfe rep_vs tmp_treat, absorb(rank year) vce(cluster rank)
			if `t'==1 reghdfe rep_vs tmp_treat, absorb(i.rank##c.year year) vce(cluster rank)
			matrix Taus[`c',1] = _b[tmp_treat] // placebo effect
			local ++c
		}
		
		* Store where the true effect falls in the placebo distribution
		cap drop Taus*
		svmat Taus
		egen placebo_rank = rank(Taus1)
		sum placebo_rank
		local ptile`g'_`t' = placebo_rank[1]/`r(max)'
		drop Taus1 placebo_rank		
		noi di "`g' `t' `ptile`g'_`t''   $S_TIME"
	}
}

* Make Table 1
quietly {
	cap log close
	log using "$path/Replication/output/fixed_effects_table.tex", text replace
	noi di "\begin{table}[t]"
	noi di "\centering"
	noi di "\caption{\textbf{Increase in Republican Presidential Vote Share in Miami-Dade County "
	noi di "Following the Mariel Boatlift, Fixed Effects Estimates.}\label{tab:fe_pres}}"
	noi di "\begin{tabular}{lcccccccccc}"
	noi di "\toprule \toprule"
	noi di " & \multicolumn{10}{c}{Rep Vote Share [0-1]} \\"
	noi di "Counties Included: & \multicolumn{2}{c}{250} & \multicolumn{2}{c}{500} "
	noi di "& \multicolumn{2}{c}{1500} & \multicolumn{2}{c}{Florida} "
	noi di "& \multicolumn{2}{c}{All Counties} \\[1mm]"
	noi di "\midrule"
	noi di " Miami $ \times $ 1980 & " %3.2f `b252_0' " & " %3.2f `b252_1' " "
	noi di " & " %3.2f `b505_0' " & " %3.2f `b505_1' " & " %3.2f `b1513_0' 
	noi di " & " %3.2f `b1513_1' " & " %3.2f `bflorida_0' " & " %3.2f `bflorida_1' " "
	noi di " & " %3.2f `b9999_0' " & " %3.2f `b9999_1' " \\"
	noi di " & [" %3.2f `ptile252_0' "] & [" %3.2f `ptile252_1' "] "
	noi di " & [" %3.2f `ptile505_0' "] & [" %3.2f `ptile505_1' "] "
	noi di " & [" %3.2f `ptile1513_0' "] & [" %3.2f `ptile1513_1' "] "
	noi di " & [" %3.2f `ptileflorida_0' "] & [" %3.2f `ptileflorida_1' "] "
	noi di " & [" %3.2f `ptile9999_0' "] & [" %3.2f `ptile9999_1' "] \\[2mm]"
	noi di "County FE & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\"
	noi di "Year FE & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes & Yes \\"
	noi di "County Trends & No & Yes & No & Yes & No & Yes & No & Yes & No & Yes \\"
	noi di "\# of Elections & " %8.0fc `e252_0' " & " %8.0fc `e252_0' " "
	noi di " & " %8.0fc `e505_0' " & " %8.0fc `e505_0' " " 
	noi di " & " %8.0fc `e1513_0' " & " %8.0fc `e1513_0' 
	noi di " & " %8.0fc `eflorida_0' " & " %8.0fc `eflorida_0' " " 
	noi di " & " %8.0fc `e9999_0' " & " %8.0fc `e9999_0' " \\"
	noi di "\# of Counties & " %8.0fc `n252_0' " & " %8.0fc `n252_0' " " 
	noi di " & " %8.0fc `n505_0' " & " %8.0fc `n505_0' " " 
	noi di " & " %8.0fc `n1513_0' " & " %8.0fc `n1513_0'
	noi di " & " %8.0fc `nflorida_0' " & " %8.0fc `nflorida_0' " "
	noi di " & " %8.0fc `n9999_0' " & " %8.0fc `n9999_0' " \\"
	noi di "\bottomrule \bottomrule"
	noi di "\multicolumn{11}{p{1\textwidth}}{\footnotesize Each cell reports an estimate "
	noi di "of the increase in support for Ronald Reagan over Jimmy Carter in the 1980 "
	noi di "presidential election. All estimates calculated holding out counties that "
	noi di "border Miami-Dade. Columns 250, 500, and 1500 are estimated with the 250, "
	noi di "500, and 1500 counties most similar to Miami-Dade in terms of log population "
	noi di "as of 1970. The share of the placebo distribution less than the effect "
	noi di "estimate is reported in square braces.}"
	noi di "\end{tabular}"
	noi di "\end{table}"
	log off
}
